
let dTime = 'date_time';

function put(k, v, t) {

  wx.setStorageSync(k, v)
  let seconds = parseInt(t);
  if (seconds > 0) {
    let timestamp = Date.parse(new Date());
    timestamp = timestamp / 1000 + seconds;
    wx.setStorageSync(k + dTime, timestamp + "")
  } else {
    wx.removeStorageSync(k + dTime)
  }
}

function get(k, def) {
  let dateTime = parseInt(wx.getStorageSync(k + dTime))
  if (dateTime) {
    if (dateTime < Date.parse(new Date()) / 1000) {
      if (def) { return def; } else { return; }
    }
  }
  let res = wx.getStorageSync(k);
  if (res) {
    return res;
  } else {
    return def;
  }
}

function remove(k) {
  wx.removeStorageSync(k);
  wx.removeStorageSync(k + dTime);
}

function clear() {
  wx.clearStorageSync();
}

module.exports = {
  put: put,
  get: get,
  remove: remove,
  clear: clear,
}
